import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_global.dart';
import 'package:flutter_learn_vi/src/common/global.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/page/login/bofang_login_main_page.dart';
import 'package:flutter_learn_vi/src/page/mine/mine_persion_info_page.dart';
import 'package:flutter_learn_vi/src/page/mine/settings_page.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';

import 'bofang/bofang_home_item1_page.dart';
import 'bofang/bofang_home_item2_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/26.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class HomeMainV3Page extends StatefulWidget {
  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<HomeMainV3Page>
    with SingleTickerProviderStateMixin {
  void streamListenFunction(GlobalBean globalBean) {
    //刷新消息
    LogUtils.e("消息传递 刷新消息 ${globalBean.data.toString()}");
    if (globalBean.code == 202) {
      //清除缓存用户信息
      UserHelper.getInstance.exitLogin();
      //跳转登录页面
      NavigatorUtils.openPageByFade(
        context,
        BoFangLoginPage(),
        mills: 1000,
        endMills: 800,
        isReplace: true,
      );
    }
  }

  StreamSubscription _homeSubscription;

  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _homeSubscription =
        rootStreamController.stream.listen(streamListenFunction);

    _tabController = new TabController(vsync: this, length: 2);
  }

  @override
  void dispose() {
    super.dispose();
    _homeSubscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Container(
        color: Colors.white,
        width: MediaQuery.of(context).size.width / 5 * 4,
        child: Container(
          padding: EdgeInsets.all(10),
          child: PersionInfoPage(
            pageIndex: 1,
          ),
        ),
      ),
      appBar: AppBar(
        title: TabBar(
          labelStyle: TextStyle(fontWeight: FontWeight.w500, fontSize: 18),
          unselectedLabelStyle:
              TextStyle(fontWeight: FontWeight.w500, fontSize: 14),
          isScrollable: true,
          controller: _tabController,
          tabs: [
            Tab(
              text: "首页面",
            ),
            Tab(
              text: "培训",
            ),
          ],
        ),
        actions: [
          IconButton(
            icon: Icon(Icons.settings),
            onPressed: () {
              NavigatorUtils.pushPage(
                context,
                SettingsPage(
                  pageIndex: 1,
                ),
              );
            },
          )
        ],
      ),
      backgroundColor: Colors.white,
      body: TabBarView(
        controller: _tabController,
        children: [
          BoFangHomeItem1Page(),
          BoFangHomeItem2Page(),
        ],
      ),
    );
  }
}
